<?php

namespace App\Http\Controllers\Admin;

use App\Model\Config;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class ConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $conf = Config::orderBy('conf_order','asc')->get();
        //格式化返回的数据
        foreach ($conf as $v){
            switch ($v->field_type){
                //文本框
                case 'input':
                    $v->conf_content = ' <input type="text" id="" name="conf_content[]" value="'.$v->conf_content.'" class="layui-input">';
                    break;
                //文本域
                case 'textarea':
                    $v->conf_content = ' <textarea name="conf_content[]" class="layui-textarea">'.$v->conf_content.'</textarea>';
                    break;
                //单选按钮
                case 'radio':
                    //定义一个字符串存放最终的拼接结果
                    $str = '';
                    $arr = explode(",",$v->field_value);
                    foreach ($arr as $value){
                        $radio_value = explode("|",$value);
                        if($radio_value[0]==$v->conf_content){
                            $str.='<input type="radio" name="conf_content[]" checked value="'.$radio_value[0].'"  title="'.$radio_value[1].'">'.$radio_value[1].'&nbsp;';
                        }else{
                            $str.='<input type="radio" name="conf_content[]" value="'.$radio_value[0].'"  title="'.$radio_value[1].'">'.$radio_value[1].'&nbsp;';
                        }

                    }
                    $v->conf_content = $str;
                    break;
                //图片
                case 'img':
                    $v->conf_content ='<img src="'.$v->conf_content.'" width="80px" height="50px" name="conf_content[]">';
                    break;
            }
        }
        return view("admin.config.list",compact("conf"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin/config/add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $res = Config::create($request->except("_token"));
        if($res){
            $this->putContent();
            $data = [
                'status' => 0,
                'message'    =>'新增成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'    =>'新增失败！'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $conf = Config::find($id);
        return view("admin.config.edit",compact("conf"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //1.根据id修改值
        $res = Config::where("conf_id",$request->input("conf_id"))->update($request->except("conf_id"));

        if($res){
            $this->putContent();
            $data = [
                'status' => 0,
                'message'=> '修改成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '修改失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $conf = Config::find($id);
        $res = $conf->delete();

        if($res){
            $this->putContent();
            $data = [
                'status' => 0,
                'message'    =>'删除成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'    =>'删除失败！'
            ];
        }
        return $data;

    }

    //批量修改网站配置
    public function changeContent(Request $request){
        $input = $request->except("_token");
        //dd($input);

        DB::beginTransaction();
        try{
            foreach ($input['conf_id'] as $k=>$v){
                DB::table('config')->where('conf_id',$v)
                    ->update(['conf_content'=>$input['conf_content'][$k]]);
            }

            DB::commit();

            $this->putContent();
            return redirect("admin/config");

        }catch (\Exception $e){
            DB::rollBack();
            return redirect()->back()
                ->withErrors(['error'=>$e->getMessage()]);
        };
    }

    //把网站配置写入config文件
    public function putContent(){

        //1.从数据表中取出网站配置
        $content = Config::pluck('conf_content','conf_name')->toArray();
        //dd($content);

        //2.准备要写入的内容
        $str = '<?php return '.var_export($content,true).';';
        //dd($str);

        //3.将内容写入文件
        file_put_contents(config_path()."/webconfig.php",$str);

    }
}
